package com.kqkj.dyrule.marketing.utils;


import com.kqkj.dyrule.marketing.beans.EventCombinationCondition;
import com.kqkj.dyrule.marketing.beans.EventCondition;
import com.kqkj.dyrule.marketing.beans.MarketingRule;

import java.util.Arrays;
import java.util.HashMap;

/**
 * 规则模拟生成器
 *  规则：
 *        触发事件： K事件，事件属性（P2=V1）
 *        画像属性条件：tag87=v2 tag26=v1   hbase
 *        行为次数条件：2022-04-17-当前  事件C[p6=v8,p12=v12] 做过>=2次 clickhouse
 */
public class RuleMonitor2 {
    public static MarketingRule getRule(){
        MarketingRule rule = new MarketingRule();
        rule.setRuleId("rule_001");
        rule.setKeyByFields("deviceId");
        //触发
        HashMap<String, String> map1 = new HashMap<>();
        //map1.put("p2","v1");
        EventCondition eventCondition = new EventCondition("K", map1, -1, Long.MAX_VALUE, 1, 999);
        rule.setTriggerEventCondition(eventCondition);
        //用户画像
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("tag87","v2");
        map2.put("tag26","v1");
        rule.setUserProfileConditions(map2);

        //单个行为次数条件列表
        String eventId = "K";
        HashMap<String, String> map3 = new HashMap<>();
        //生成的规则次数条件附带ck查询sql
        long startTime = -1L;
        long endTime = Long.MAX_VALUE;
        String sql = "select count(1) from zenniu_detail " +
                "where eventId='K' and properties['p6']='v1' and properties['p1']='v1' " +
                "and deviceId=? and timeStamp between ? and ? ";
        String rPattern1 = "(1)";
        EventCondition e = new EventCondition(eventId, map3, startTime, endTime, 1, 999);
        EventCombinationCondition eventGroupParam = new EventCombinationCondition(startTime,endTime,Arrays.asList(e),rPattern1,1,999,"ck",sql,"");

        //行为组合条件
        long st = -1L;
        long ed = Long.MAX_VALUE;
        String eventId1 = "A";
        HashMap<String, String> m1 = new HashMap<>();
        EventCondition e1 = new EventCondition(eventId1, m1, st, ed, 1, 999);

        String eventId2 = "C";
        HashMap<String, String> m2 = new HashMap<>();
        EventCondition e2 = new EventCondition(eventId2, m2, st, ed, 1, 999);

        String eventId3 = "F";
        HashMap<String, String> m3 = new HashMap<>();
        EventCondition e3 = new EventCondition(eventId2, m3, st, ed, 1, 999);

        String sql2 = "select eventId from zenniu_detail where deviceId= ? and timeStamp between ? and ? " +
                "and ((eventId='A') or (eventId='C') or (eventId='F'))";
        String rPattern2 = "(1.*2.*3)";
        EventCombinationCondition eventGroupParam2 = new EventCombinationCondition(st, ed, Arrays.asList(e1, e2, e3), rPattern2, 1, 999, "ck", sql2, "");
        rule.setEventCombinationConditionList(Arrays.asList(eventGroupParam,eventGroupParam2));

        return rule;
    }
}
